﻿<?php require_once('../Connections/conexao.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}


$colname_veriSubcategoria = "-1";
if (isset($_GET['idsubcategoria'])) {
  $colname_veriSubcategoria = $_GET['idsubcategoria'];
}
mysql_select_db($database_conexao, $conexao);
$query_veriSubcategoria = sprintf("SELECT COUNT(*) AS total FROM produtos WHERE id_subcategoria = %s", GetSQLValueString($colname_veriSubcategoria, "int"));
$veriSubcategoria = mysql_query($query_veriSubcategoria, $conexao) or die(mysql_error());
$row_veriSubcategoria = mysql_fetch_assoc($veriSubcategoria);
$totalRows_veriSubcategoria = mysql_num_rows($veriSubcategoria);
 
if ( 0 == $row_veriSubcategoria['total'] ){
	if ((isset($_POST['idsubcategoria'])) && ($_POST['idsubcategoria'] != "") && (isset($_GET['idsubcategoria']))) {
  $deleteSQL = sprintf("DELETE FROM subcategorias WHERE id_subcategoria=%s",
                       GetSQLValueString($_POST['idsubcategoria'], "int"));

  mysql_select_db($database_conexao, $conexao);
  $Result1 = mysql_query($deleteSQL, $conexao) or die(mysql_error());

  $deleteGoTo = "subcategoria.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
    $deleteGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $deleteGoTo));
}

}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Documento sem título</title>
</head>


<body>
<table width="960" align="center">
  <tr>
    <td colspan="2">
	<?php include ("../temadmin/cabecalho.php");?>

</td>
  </tr>
  <tr>
    <td colspan="2">
	<?php include "../temadmin/menu_vertical.php";
	?>
    </td>
  </tr>
  <tr>
    <td width="250">
	<?php include "../temadmin/menu.php";?>
    </td>
    <td width="698">
   		<table width="99%" align="center">
  <tr>
    <td>      <table width="99%">
        <tr>
          <th width="20%" scope="col">&nbsp;</th>
          <th colspan="3" scope="col">Excluir Subcategoria</th>
          <th width="20%" scope="col">&nbsp;</th>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td width="8%">&nbsp;</td>
          <td width="49%">&nbsp;</td>
          <td width="3%">&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td><?php if ( 0 == $row_veriSubcategoria['total'] ) : ?>
            <form method="post" action="">
              <p><font face="Verdana, Geneva, sans-serif"><strong>Você realmente deseja excluir esta Subcategoria?</strong></font></p>
  <p>
    <input type="submit" value="Sim" />
    <input type="hidden" name="idsubcategoria" value="<?php echo $_GET['idsubcategoria']; ?>" />
    </p>
            </form>
  <?php else : ?>
            <p><font face="Verdana, Geneva, sans-serif"><strong>A categoria em questão está em uso. Portanto, ela não poderá ser excluída.</strong></font></p>
  <?php endif; ?></td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        </table>
      
    </td>
  </tr>
</table>

    </td>
  </tr>
  <tr>
    <td colspan="2">
	<?php include ("../temadmin/rodape.php");?>
    </td>
  </tr>
</table>

</body>
</html>
<?php
mysql_free_result($veriSubcategoria);
?>
